package AddProduct;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class InsertIntoClothingInventory
 * This servlet insert data into CLOTHING_INVENTORY table in database.
 * @author desso
 */
@WebServlet("/InsertIntoClothingInventory")
public class InsertIntoClothingInventory extends HttpServlet {
	private static final long serialVersionUID = 1L;

    /**
     * Default constructor. 
     */
    public InsertIntoClothingInventory() {
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 HttpSession sess = request.getSession();	
			
		    try {
		    	Connection databaseConnection = SQL.getConnection();				
				insertIntoClothingInventoryr(request, sess, databaseConnection);	
				HTML.PrintASuccessMessage(response, "product"); 			
			} catch (SQLException | ClassNotFoundException e) {
				e.printStackTrace();
			}
		}

		private void insertIntoClothingInventoryr(HttpServletRequest request, HttpSession sess, Connection dbConnection) throws SQLException {
			PreparedStatement prepareStatement = dbConnection.prepareStatement(SQLStatements.INSERT_INTO_CLOTHING_INVENTORY_TABLE);
			prepareStatement.setLong(1, (Long) sess.getAttribute("item_id"));
			prepareStatement.setString(2, request.getParameter("size"));
			prepareStatement.setString(3, request.getParameter("color"));
			prepareStatement.setString(4, request.getParameter("count"));
			prepareStatement.executeUpdate();
		}
}
